package com.datastructure.test.recall.mergesortedlinkedlist;

import java.util.ArrayList;
import java.util.Collections;

public class MergedSortedLinkedList {

    ArrayList<Integer> res = new ArrayList<>();
    public static void main(String[] args) {

    }

    public ListNode mergeKLists(ArrayList<ListNode> lists) {
        generateRes(lists);
        Collections.sort(res);
        ListNode cur = null;
        ListNode temp = null;
        for (int i = res.size() - 1; i >= 0; i--) {
            cur=new ListNode(res.get(i));
            cur.next = temp;
            temp = cur;
        }
        return cur;
    }

    public void generateRes(ArrayList<ListNode> lists){
        for (ListNode list : lists) {
            while (list!=null){
                res.add(list.val);
                list=list.next;
            }
        }
    }
}

class ListNode {
      int val;
      ListNode next;
      ListNode(int x) {
          val = x;
          next = null;
      }
}
